package com.pureut.system.service.impl;

import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.PasswordVerify;
import com.pureut.system.domain.dto.PasswordVerifyDto;
import com.pureut.system.domain.vo.PasswordVerifyVo;
import com.pureut.system.mapper.PasswordVerifyMapper;
import com.pureut.system.service.PasswordVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/***
 *
 * Author:C
 * Date:2022/12/14 9:38
 * @DESC
 */
@Service
public class PasswordVerifyServiceImpl implements PasswordVerifyService {

    @Autowired
    private PasswordVerifyMapper passwordVerifyMapper;


    /**
     * 查询密码校验列表
     *
     * @param passwordVerifyVo
     * @return
     */
    @Override
    public PasswordVerifyDto selectPasswordVerifyList(PasswordVerifyVo passwordVerifyVo) {

        return passwordVerifyMapper.selectPasswordVerifyList(passwordVerifyVo);
    }

    /**
     * 新增密码校验
     *
     * @param passwordVerifyVo
     * @return
     */
    @Override
    public boolean verify(PasswordVerifyVo passwordVerifyVo) {

        boolean lengthPasswd = passwordVerifyVo.getPasswordLength() <= passwordVerifyVo.getInitialPassword().length();

        // 定义密码复杂度
        boolean isLow = false;
        boolean isUp = false;
        boolean isChar = false;
        // 判断设置的密码长度是否和初始密码长度一致
        if (lengthPasswd) {
            //判断是否包含字母
            String regex = ".*[a-z].*";
            if (passwordVerifyVo.getContainsLetters() == 1) {
                if (passwordVerifyVo.getInitialPassword().matches(regex)) {
                    isLow = true;
                } else {
                    throw new GlobalException("密码中不包含字母");
                }
            }

            //判断是否包含大写字母
            String regexUp = ".*[A-Z].*";
            if (passwordVerifyVo.getContainsUpLetters() == 1) {
                if (passwordVerifyVo.getInitialPassword().matches(regexUp)) {
                    isUp = true;
                } else {
                    throw new GlobalException("密码中不包含大写字母");
                }
            }
            //判断是否包含特殊字符
            String regexChar = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
            boolean isContain = Pattern.compile(regexChar).matcher(passwordVerifyVo.getInitialPassword()).find();
            if (passwordVerifyVo.getContainsCharacters() == 1) {
                if (isContain) {
                    isChar = true;
                } else {
                    throw new GlobalException("密码中不包含特殊字符");

                }
            }
            // 根据密码复杂度进行判断存储数据
            if (isLow || isUp || isChar) {
                PasswordVerify passwordVerify = new PasswordVerify();
                BeanUtils.copyProperties(passwordVerifyVo, passwordVerify);
                passwordVerify.setId(passwordVerifyVo.getId());
                passwordVerify.setCreateBy(SecurityUtils.getUsername());
                passwordVerify.setCreateTime(new Date());
                passwordVerify.setPasswordLength(passwordVerifyVo.getPasswordLength());
                passwordVerify.setContainsLetters(passwordVerifyVo.getContainsLetters());
                passwordVerify.setContainsCharacters(passwordVerifyVo.getContainsCharacters());
                passwordVerify.setContainsUpLetters(passwordVerifyVo.getContainsUpLetters());
                passwordVerify.setInitialPassword(passwordVerifyVo.getInitialPassword());
                return passwordVerifyMapper.updatePassword(passwordVerify);
            }
            // 判断密码复杂度是否全部为未勾选（1、勾选,2、未勾选）
            if (passwordVerifyVo.getContainsLetters() == 2 && passwordVerifyVo.getContainsUpLetters() == 2 && passwordVerifyVo.getContainsCharacters() == 2) {
                PasswordVerify passwordVerify = new PasswordVerify();
                BeanUtils.copyProperties(passwordVerifyVo, passwordVerify);
                passwordVerify.setId(passwordVerifyVo.getId());
                passwordVerify.setCreateBy(SecurityUtils.getUsername());
                passwordVerify.setCreateTime(new Date());
                passwordVerify.setPasswordLength(passwordVerifyVo.getPasswordLength());
                passwordVerify.setContainsLetters(passwordVerifyVo.getContainsLetters());
                passwordVerify.setContainsCharacters(passwordVerifyVo.getContainsCharacters());
                passwordVerify.setContainsUpLetters(passwordVerifyVo.getContainsUpLetters());
                passwordVerify.setInitialPassword(passwordVerifyVo.getInitialPassword());
                return passwordVerifyMapper.updatePassword(passwordVerify);
            }
        } else {
            throw new GlobalException("密码长度不符合密码管控设置的长度");
        }
        return false;
    }

    /**
     * 判断密码条件
     *
     * @param passwordVerifyVo
     * @return
     */
    @Override
    public boolean updateVerifyPassword(PasswordVerifyVo passwordVerifyVo) {
        boolean lengthPasswd = passwordVerifyVo.getPasswordLength() <= passwordVerifyVo.getInitialPassword().length();

        // 定义密码复杂度
        boolean isLow = false;
        boolean isUp = false;
        boolean isChar = false;
        // 判断设置的密码长度是否和初始密码长度一致
        if (lengthPasswd) {
            //判断是否包含字母
            String regex = ".*[a-z].*";
            if (passwordVerifyVo.getContainsLetters() == 1) {
                if (passwordVerifyVo.getInitialPassword().matches(regex)) {
                    isLow = true;
                } else {
                    throw new GlobalException("密码中包含字母");
                }
            }

            //判断是否包含大写字母
            String regexUp = ".*[A-Z].*";
            if (passwordVerifyVo.getContainsUpLetters() == 1) {
                if (passwordVerifyVo.getInitialPassword().matches(regexUp)) {
                    isUp = true;
                } else {
                    throw new GlobalException("密码中包含大写字母");
                }

            }
            //判断是否包含特殊字符
            String regexChar = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
            boolean isContain = Pattern.compile(regexChar).matcher(passwordVerifyVo.getInitialPassword()).find();
            if (passwordVerifyVo.getContainsCharacters() == 1) {
                if (isContain) {
                    isChar = true;
                } else {
                    throw new GlobalException("密码中包含特殊字符");
                }

            }
            // 根据密码复杂度进行判断存储数据
            if (isLow || isUp || isChar) {
                return true;
            }
            // 判断密码复杂度是否全部为未勾选（1、勾选,2、未勾选）
            if (passwordVerifyVo.getContainsLetters() == 2 && passwordVerifyVo.getContainsUpLetters() == 2 && passwordVerifyVo.getContainsCharacters() == 2) {
                return true;
            }
        } else {
            return false;
        }
        return false;
    }

    /**
     * 查询密码校验列表(修改密码模块用)
     *
     * @return
     */
    @Override
    public PasswordVerifyDto getPasswordVerifyInfo() {
        return passwordVerifyMapper.getPasswordVerifyInfo();
    }

}
